<template>
    <div>
        <el-table
            :data="
                adminsData.filter(
                    (data) =>
                        !search ||
                        data.account
                            .toLowerCase()
                            .includes(search.toLowerCase())
                )
            "
            style="width: 100%"
            :fit="true"
        >
            <el-table-column type="index" label="编号" width="300px">
            </el-table-column>
            <el-table-column label="账号" prop="account"> </el-table-column>
            <el-table-column label="修改密码">
                <template slot-scope="scope">
                    <el-button type="text" @click="open(scope.row._id,scope.row.account)"
                        >修改密码</el-button
                    >
                </template>
            </el-table-column>
            <el-table-column label="冻结">
                <template slot-scope="scope">
                    <el-switch
                        v-model="scope.row.state"
                        active-text="冻结"
                        inactive-text="解冻"
                        active-value="2"
                        inactive-value="1"
                        @change="
                            frozenAdminAndCreateLogs({
                                _id: scope.row._id,
                                state: scope.row.state,
                                account:scope.row.account
                            })
                        "
                    >
                    </el-switch>
                </template>
            </el-table-column>
            <el-table-column align="right">
                <template slot="header" slot-scope="scope">
                    <el-input
                        v-model="search"
                        size="mini"
                        placeholder="请输入账号关键字"
                    />
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="getData.currentpage"
      :page-sizes="[5, 7, 10, 15]"
      :page-size="getData.pagesize-0"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total-0">
    </el-pagination>
    <!-- 新增管理员 -->
        <el-button
            type="success"
            icon="el-icon-circle-plus-outline"
            circle
            @click="dialogFormVisible = true"
            width='50px'
        ></el-button>
        <el-dialog title="新增管理员" :visible.sync="dialogFormVisible">
            <el-form :model="form">
                <el-form-item label="账号" :label-width="formLabelWidth">
                    <el-input
                        v-model="newAdmin.account"
                        autocomplete="off"
                    ></el-input>
                </el-form-item>
                <el-form-item label="密码" :label-width="formLabelWidth">
                    <el-input
                        v-model="newAdmin.password"
                        autocomplete="off"
                    ></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button
                    type="primary"
                    @click="
                        dialogFormVisible = false;
                        addAdmin({
                            account: newAdmin.account,
                            password: newAdmin.password,
                        });
                    "
                    >确 定</el-button
                >
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { createNamespacedHelpers } from "vuex";
import axios from '../../http/axios.js'
const { mapState, mapActions, mapMutations } = createNamespacedHelpers(
    "admins"
);
export default {
    props:['token'],
    data() {
        return {
            search: "",
            updatePasswordId: "",
            adminState: "",
            newAdmin: {
                account: "",
                password: "",
            },
            dialogTableVisible: false,
            dialogFormVisible: false,
            form: {
                name: "",
                region: "",
                date1: "",
                date2: "",
                delivery: false,
                type: [],
                resource: "",
                desc: "",
            },
            formLabelWidth: "120px",
            getData:{
            currentpage:1,
            pagesize:5
        }
        };
    },
    watch: {
        search(val, oldValue) {
            console.log(val, oldValue);
        }
    },
    mounted() {
        this.getAdmins(this.getData);
    },
    computed: {
        ...mapState(["adminsData",'total']),
        
    },
    methods: {
        handleSizeChange(val) {
        this.getData.pagesize=val
        console.log(this.getData);
        this.getAdmins(this.getData);
      },
      handleCurrentChange(val) {
        this.getData.currentpage=val
        this.getAdmins(this.getData);
      },
        ...mapActions([
            "getAdmins",
            "updateAdminPassword",
            "frozenAdmin",
            "addNewAdmin",
        ]),
        //新增管理员并创建日志
        addAdmin(data){
            this.addNewAdmin(data)
           
             axios({
                    url:'/logs/createLogs',
                    method:'post',
                    data:{
                        name:this.token,
                        operation:'新增管理员'+data.account,
                        result:'成功'
                    }
                    })
        },
        //冻结用户并创建日志
        frozenAdminAndCreateLogs(data){
            this.frozenAdmin(data)
             console.log(data);
            if(data.state==1){
                 axios({
                    url:'/logs/createLogs',
                    method:'post',
                    data:{
                        name:this.token,
                        operation:'解冻管理员'+data.account,
                        result:'成功'
                    }
                    })
            }else{
                 axios({
                    url:'/logs/createLogs',
                    method:'post',
                    data:{
                        name:this.token,
                        operation:'冻结管理员'+data.account,
                        result:'成功'
                    }
                    })
            }
        },
        open(_id,name) {
            axios({
                    url:'/logs/createLogs',
                    method:'post',
                    data:{
                        name:name,
                        operation:'超管修改密码',
                        result:'成功'
                    }
                    })
            this.updatePasswordId = _id;
            this.$prompt("请输入新密码", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                inputPattern: /\w/,
                inputErrorMessage: "密码格式不正确",
            })
                .then(({ value }) => {
                    this.updateAdminPassword({
                        password: value,
                        _id: this.updatePasswordId,
                    });
                    console.log(this.updatePasswordId);
                    this.$message({
                        type: "success",
                        message: "修改密码成功，新密码为: " + value,
                    });
                })
                .catch(() => {
                    this.$message({
                        type: "info",
                        message: "取消输入",
                    });
                });
        },
    },
};
</script>